﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class StateMachineTransitionELBDao : DaoService<TTransition>
    {
        public override string TableName => "FAS_StateMachineTransition";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "StateMachineID";

        public override List<string> ExceptColumnName { get; } = DaoUtil.GetExceptionColumn<TTransition>();

        public StateMachineTransitionELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<TTransition> GetTTransitionsByForeignKey(string smID)
        {
            var result = new List<TTransition>();
            if (string.IsNullOrEmpty(smID))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{smID}'");
            if (objects != null)
            {
                foreach (var obj in objects)
                {
                    var tran = obj as TTransition;
                    //tran.Actions = stateMachineActionELBDao.GetTActions(tran.ID)?.OrderBy(r => r.Sort).ToList();
                    result.Add(tran);
                }
            }
            return result.OrderBy(o => o.Sort).ToList();
        }
 
        /// <summary>
        /// 添加TTransition
        /// </summary>
        /// <param name="trans"></param>
        public void CreateTTransition(List<TTransition> trans)
        {
            try
            {
                DaoUtil.ExecuteInsertSQL(trans, this);    //创建状态变换
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.CreateTransError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除TTransition
        /// </summary>
        /// <param name="TTransitions"></param>
        public void DeleteTTransitionByForeignKey(string smID)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, smID));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.DeleteTransError, e.Message, e);
            }
        }

    }
}
